﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace B.X.Rabbit.Infrastructure
{
    /// <summary>
    /// 仓储
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class BaseReponsitory<T> : IBaseReponsitory<T> where T : class
    {
        /// <summary>
        /// 上下文
        /// </summary>
        private readonly MainDbContext db;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="db"></param>
        public BaseReponsitory(MainDbContext db)
        {
            this.db = db;
        }


        /// <summary>
        /// 获取全部信息
        /// </summary>
        /// <returns>返回数据集合</returns>
        public IQueryable<T> GetAll()
        {
            return db.Set<T>().AsQueryable();
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity">命令</param>
        /// <returns>返回数据集合</returns>
        public int Add(T entity)
        {
            db.Set<T>().Add(entity);
            return db.SaveChanges();
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity">命令</param>
        /// <returns>返回数据集合</returns>
        public int Update(T entity)
        {
            db.Set<T>().Update(entity);
            return db.SaveChanges();
        }
        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="entities">命令</param>
        /// <returns>返回数据集合</returns>
        public int AddRange(List<T> entities)
        {
            db.Set<T>().AddRange(entities);
            return db.SaveChanges();
        }
        /// <summary>
        /// 批量更新
        /// </summary>
        /// <param name="entities">命令</param>
        /// <returns>返回数据集合</returns>
        public int UpdateRange(List<T> entities)
        {
            db.Set<T>().UpdateRange(entities);
            return db.SaveChanges();
        }
    }
}
